package org.fgtv.gospel.user.dao;

import java.util.List;

import org.fgtv.gospel.domain.User;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class UserHibernateDao extends HibernateDaoSupport implements UserDao {
	
//	private static Logger log = Logger.getLogger(UserHibernateDao.class);
	
	@Override
	public void add(User user) {
		getHibernateTemplate().save(user);
	}

	@Override
	public User get(String userId) {
		
		return (User) getSession().createQuery("FROM User WHERE userId = :userId")
				.setString("userId", userId)
				.uniqueResult();
	}

	@Override
	public List<User> getAll() {
		
		Query query = getSession().createQuery("FROM User");
		return query.list();
	}

	@Override
	public void deleteAll() {

		getSession().createQuery("DELETE FROM User").executeUpdate();
	}

	@Override
	public int getCount() {
		return ((Long) getSession().createQuery("SELECT COUNT(*) FROM User").iterate().next()).intValue();
	}

	@Override
	public void update(User user) {
		getHibernateTemplate().update(user);
	}

}
